/*
 * Copyright (c) Created by T C L on 2019.2.27.
 */

package com.egg.ylt.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.pojo.CouponEntity;
import com.egg.ylt.presenter.impl.CardConfirmPaymentPresenterImpl;
import com.egg.ylt.view.ICardConfirmPaymentView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_ConfirmPayment extends BaseActivity<CardConfirmPaymentPresenterImpl> implements ICardConfirmPaymentView {

    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.include_tv_title)
    TextView mTitleTv;

    @BindView(R.id.store_name_tv)
    TextView mStoreNameTv;

    @BindView(R.id.service_img_iv)
    ImageView mServiceImgIv;

    @BindView(R.id.meal_card_name_tv)
    TextView mMealCardNameTv;

    @BindView(R.id.validity_period_tv)
    TextView mValidityPeriodTv;

    @BindView(R.id.price_tv)
    TextView mPriceTv;

    @BindView(R.id.count_tv)
    TextView mCountTv;

    @BindView(R.id.total_price_tv)
    TextView mTotalPriceTv;

    @BindView(R.id.coupon_tv)
    TextView mCouponTv;

    @BindView(R.id.real_pay_tv)
    TextView mRealPayTv;

    @BindView(R.id.phone_number_tv)
    TextView mPhoneNumberTv;

    @BindView(R.id.pay_tv)
    TextView mPayTv;


    private String mCardId;
    private String mShopName;
    private String mCardName;
    private String mPrice;
    private String mEndDate;
    private String mCardImageUrl;
    private String mTicketId;
    private String mTicketAmount;
    private String mApplyType;//优惠券类型 0-商品，1-服务，2-次卡 “”所有
    private String mId;
    private String mShopId;


    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        mCardId = extras.getString("cardId");
        mShopName = extras.getString("shopName");
        mCardName = extras.getString("cardName");
        mPrice = extras.getString("price");
        mEndDate = extras.getString("endDate");
        mCardImageUrl = extras.getString("cardImageUrl");
        mApplyType = extras.getString("applyType");
        mShopId = extras.getString("shopId");

        //获取本地用户数据
        AppSharedPreferences sharedPreferences = new AppSharedPreferences(this);
        mPresenter.getCouponList(sharedPreferences.getString(Constants.USER_TOKEN), Constants.COMPANYID,
                sharedPreferences.getString(Constants.USER_ID), mApplyType, mShopId, mCardId);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_confirm_payment;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        mTitleTv.setText("确认支付");

        mStoreNameTv.setText(mShopName);

        Glide.with(mContext).load(mCardImageUrl).placeholder(R.mipmap.ic_serve_default).error(R.mipmap.ic_serve_default).dontAnimate().into(mServiceImgIv);

        mMealCardNameTv.setText(mCardName);

        mPriceTv.setText(StringUtil.getBigDecimalPrice(mPrice));

        mTotalPriceTv.setText(StringUtil.getBigDecimalPrice(mPrice));

        mValidityPeriodTv.setText("有效期至" + mEndDate);

        mRealPayTv.setText(StringUtil.getBigDecimalPrice(mPrice));

        AppSharedPreferences sharedPreferences = new AppSharedPreferences(mContext);
        mPhoneNumberTv.setText(sharedPreferences.getString(Constants.USER_PHONE).replaceAll("(?<=\\d{3})\\d" +
                "(?=\\d{4})", "*"));

        mPayTv.setText("去支付 ¥" + StringUtil.getBigDecimalPrice(mPrice));
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick(value = {R.id.coupon_rl, R.id.pay_tv, R.id.include_iv_back})
    public void toggleEvent(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back:
                finish();
                break;
            case R.id.coupon_rl:
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                Intent intent = new Intent(mContext, ACT_Coupons.class);
                intent.putExtra("payActivityEnter", true);
                intent.putExtra("cardId", mCardId);
                intent.putExtra("totalPrice", mPrice);
                intent.putExtra("applyType", mApplyType);
                intent.putExtra("shopId", mShopId);
                startActivityForResult(intent, 1);
                break;
            case R.id.pay_tv:
                Intent i = new Intent(this, ACT_Payment.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("id", mId);
                bundle.putSerializable("cardId", mCardId);
                bundle.putSerializable("price", mRealPayTv.getText().toString());
                bundle.putSerializable("cardName", mCardName);
                bundle.putString("ticketId", mTicketId);
                bundle.putString("ticketAmount", mTicketAmount);
                i.putExtras(bundle);
                startActivity(i);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            mId = data.getStringExtra("id");
            mTicketAmount = data.getStringExtra("ticketAmount");
            mTicketId = data.getStringExtra("ticketId");
            mCouponTv.setText("-¥" + StringUtil.getBigDecimalPrice(mTicketAmount));
            mRealPayTv.setText(StringUtil.getBigDecimalPrice(Double.parseDouble(mPrice) - Double.parseDouble(mTicketAmount) + ""));
            mPayTv.setText("去支付 ¥" + StringUtil.getBigDecimalPrice(Double.parseDouble(mPrice) - Double.parseDouble(mTicketAmount) + ""));
        }
    }

    @Override
    public void resolveCouponLogic(CouponEntity entity) {
        if (entity == null || entity.getList().isEmpty()) {
            mCouponTv.setText("暂无可用");
            return;
        }

        int availableCount = 0;
        for (int i = 0; i < entity.getList().size(); i++) {
            if (Double.parseDouble(mPrice) > Double.parseDouble(entity.getList().get(i).getTicketAmount()) && Double.parseDouble(mPrice) >= Double.parseDouble(entity.getList().get(i).getLimitAmount())) {
                availableCount++;
            }
        }

        if (availableCount == 0) {
            mCouponTv.setText("暂无可用");
        } else {
            mCouponTv.setText("可用" + availableCount + "张");
        }

    }
}
